package myservlet.control;
import mybean.data.shopContent_Bean;
import mybean.data.Account_Bean;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/shopContent")
public class shopContent_Servlet extends HttpServlet{
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        try {  Class.forName("com.mysql.jdbc.Driver");
        }
        catch(Exception e){}
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        request.setCharacterEncoding("gb2312");
        String dataBase= "web_used";
        String tableName= request.getParameter("tableName");
//        测试输出
//        System.out.println(tableName);

        HttpSession session=request.getSession(true);
        Connection con=null;
        shopContent_Bean pageBean=null;
        Account_Bean LoginBean=null;
        //存放商品内容的bean
        try{
            pageBean=(shopContent_Bean)session.getAttribute("pageBean");
            if(pageBean==null){
                pageBean=new shopContent_Bean();  //创建Javabean对象
                session.setAttribute("pageBean",pageBean);
            }
        }
        catch(Exception exp){
            pageBean=new shopContent_Bean();
            session.setAttribute("pageBean",pageBean);
        }

        //用户信息的bean
        try{
            LoginBean=(Account_Bean)session.getAttribute("LoginBean");
            if(LoginBean==null){
                LoginBean=new Account_Bean();  //创建Javabean对象
                session.setAttribute("LoginBean",LoginBean);
            }
        }
        catch(Exception exp){
            LoginBean=new Account_Bean();
            session.setAttribute("LoginBean",LoginBean);
        }

        //判断当前是否是登录状态
        if(LoginBean.getUsername().equals("") || LoginBean.getPassword().equals("")){
            fail(request, response, "请您先登录在查看丰富的商品");
            //没有登录, 不给查看
            return;
        }

        String uri="jdbc:mysql://127.0.0.1/"+dataBase;
        try{
            con=DriverManager.getConnection(uri,"root","root");
            Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet rs=sql.executeQuery("SELECT * FROM "+tableName);
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount(); //得到结果集的列数
            String []columnName = new String[columnCount];
            for(int i=0;i<columnName.length;i++) {
                columnName[i] = metaData.getColumnName(i+1); //得到列名
            }
            pageBean.setColumnName(columnName);   //更新Javabean数据模型
            pageBean.setShopType(tableName);



            rs.last();
            int rowNumber=rs.getRow();  //得到记录数
            String [][] tableRecord=pageBean.getTableRecord();
            tableRecord = new String[rowNumber][columnCount];
            rs.beforeFirst();
            int i=0;
            while(rs.next()){
                for(int k=0;k<columnCount;k++)
                    tableRecord[i][k] = rs.getString(k+1);
                i++;
            }
            pageBean.setTableRecord(tableRecord); //更新Javabean数据模型
            con.close();
            response.sendRedirect("/Vice_School_ShopWeb_war_exploded/shop_module/shopShow.jsp");  //重定向
        }
        catch(SQLException e){
            System.out.println(e);
        }
    }
    public  void  doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doPost(request,response);
    }
    public void fail(HttpServletRequest request,HttpServletResponse response,
                     String backNews) {
        response.setContentType("text/html;charset=GB2312");
        try {
            PrintWriter out=response.getWriter();
            out.println("<html>");
            out.println("<head><style>body{text-align: center;background: #ea66a6;margin:300px}</style></head>");
            out.println("<body>");
            out.println("<h2>"+backNews+"</h2>") ;
            out.println("返回");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/member/login.jsp>登录页面</a>");
            out.println("</body></html>");
        }
        catch(IOException exp){}
    }

}
